// pages/event/reporting/reportingdetails/supplement/supplement.js
import Toast from "../../../../../miniprogram_npm/@vant/weapp/toast/toast";
import Dialog from "../../../../../miniprogram_npm/@vant/weapp/dialog/dialog";
const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    event_id: null,
    fileList: [],
    textareaValue: null,//描述信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      event_id: options.event_id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
 * 图片上传
 */
  afterRead: function (e) {
    const than = this
    for (let i = 0; i < e.detail.file.length; i++) {
      than.data.fileList.push({
        url: e.detail.file[i].path,
        name: e.detail.file[i].size
      })
    }
    than.setData({
      fileList: than.data.fileList
    })
  },
  /**
    * 删除图片
    */
  deleteImage: function (e) {
    const than = this
    const index = e.detail.index
    const deleteData = this.data.fileList.splice(index, 1)
    than.setData({
      fileList: than.data.fileList
    })

  },
  /**
   * 设置事件描述 输入框中的值
   */
  getTextareaValue: function (e) {
    this.setData({
      textareaValue: e.detail.value
    });
  },
  setSupplementEvent: function (e) {
    const than = this
    const fileList1 = than.data.fileList
    let fileArray = []
    let isFile = true
    let fileNum = 0
    //上传附件
    if (fileList1 != null) {
      for (let i = 0; i < fileList1.length; i++) {
        http.upFile(fileList1[i].url, "event")
          .then((res) => {
            fileArray.push({
              attachment_name: res.data.name,
              attachment_url: res.data.url,
              attachment_type: 1
            })
            fileNum += 1
          }, err => {
            fileNum += 1
          })
      }
      if (than.data.textareaValue != null) {
        Dialog.confirm({
          title: "进度汇报",
          message: "确定要汇报进度吗？"
        }).then(() => {
          Toast.loading({
            duration: 0,
            forbidClick: true,
            message: '正在上报，请稍等！'
          })
          var interval = setInterval(res => {
            if (fileNum >= than.data.fileList.length && isFile) {
              clearInterval(interval);
              http.putJson("event/info/handle", {
                content: than.data.textareaValue,
                attachment_list: fileArray,
                event_id: than.data.event_id,
                action: "2"
              }).then((res) => {
                if (res.data.code == 200) {
                  //请求成功上传附件
                  Toast.success({
                    message: res.data.msg
                  })
                  setTimeout(() => {
                    Toast.clear()
                    wx.navigateBack({
                      delta: 2
                    })
                  }, 2000)
                } else {
                  Toast.fail({
                    message: "提交失败，错误代码：" + res.data.msg + ",请联系管理员!"
                  })
                }
              })
            }
          }, 1000)
        }).catch(() => {

        })
      } else {
        Toast.fail({
          message: '请事件补充信息！'
        })
      }
    }
  }
})